const request=require('../../util/request.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        viewList:[],
        mindate:Math.round((new Date() - 365*86400*1000)),
        showstarttime:false,
        showlasttime:false,
        selDay:'',//领料时间
        bumen_name:'',//部门名字
        bumen_id:'',//部门id
        entry:'',
    },
    refreshCalendar(type) {
      this.setData({
        [`timestamp${type}`]: new Date().getTime()
      })
    },
    onDisplay(event) {
      this.data.activeId=event.currentTarget.dataset.id
      this.setData({ showstarttime: true });
    },
    onDisplays(event) {
      this.data.activeId=event.currentTarget.dataset.id
      this.setData({ showlasttime: true });
    },
    onClose() {
      this.setData({ showstarttime: false });
      this.setData({ showlasttime: false });
    },
    start(event) {
      console.log(event,"aaa")
        const tmptime = this.formatDate(event.detail)
        this.setData({
          selDay:tmptime
        })
        this.onClose()
    },
    setUse(e){
      console.log(e,"eee")
      this.setData({
        setUse:e.detail
      })
    },

    setDept(e){
      wx.navigateTo({
        url: `/pages/sel_dept/sel_dept`,
      })
    },

    last(event) {
      const tmptime = this.formatDate(event.detail)
      let tmpNum = this.data.activeId
      var arrName = event.currentTarget.dataset.arrName;
      var variable = event.currentTarget.dataset.variable;
      var arr = this.data[arrName];
      arr[tmpNum][variable] = tmptime
      this.setData({
          [arrName]:arr
      })
      this.onClose()
    },

    formatDate(date) {
      date = new Date(date);
      let tmp = `${date.getMonth()+1}`
      if(tmp<10){
        tmp = 0+tmp
      }
      return `${date.getFullYear()}-`+tmp+`-${date.getDate()}`;
    },
    formatDates(date) {
      date = new Date(date);
      return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}`;
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
     
    },
    submit1(event) {
      const callback = () =>{
        wx.navigateBack()
      }
      let tmp = {
        FDate:this.data.selDay,
        FUse:this.data.setUse,
        FDeptID:this.data.bumen_id,
        entry:this.data.entry
      }
      request.requestAjax("material/generate_ticket",tmp,"上传中",function(res){
          if (res.status === 200) {
            wx.showToast({
            title: '提交成功',
            icon: 'success',
            success:callback
          });
          
        }
      },
        function (res) {
          wx.showToast({
            title: '错误',
            icon:'none'
          })
      })
    },

    //用于 数组 对象 具体属性值的同步修改 
    bindKeyInputVant1(event) {
        var arrName = event.currentTarget.dataset.arrName;
        var index = event.currentTarget.dataset.index;
        var variable = event.currentTarget.dataset.variable;
        var arr = this.data.viewList;
        arr[index][variable] = event.detail
        this.setData({
            [arrName]:arr
        })
        //NOTE 最终查看写入结果
        console.log(this.data.viewList)
    },
    delData(data){
      let currentList = this.data.viewList
      //NOTE 获取传来的index
      var index = data.currentTarget.dataset.index;
      currentList.splice(index, 1)
        this.setData({
            viewList: currentList
        })
    },

    delRow(event){
      var that = this;
      wx.showModal({
        title: '提示',
        content: '确定删除当前数据吗？',
        success: function (res) {
          if (res.confirm) {
            that.delData(event)
          } 
        }
      })
    },
    selsel_wuliao(event){
      var index = event.currentTarget.dataset.index
      wx.navigateTo({
        url: `/pages/selsel_wuliao/selsel_wuliao?index=${index}`,
      })
    },

    setManager(){
      if (this.data.__returnParams) {
        this.setData({ 
          bumen_name:this.data.__returnParams[1],
          bumen_id:this.data.__returnParams[0],
          __returnParams: null 
        })
      }
    },

    addNewView() {
      let currentList = this.data.viewList
      currentList.push({FQty:"",FItemID:'',FBatchNo:"",FUnitID:'',sel_wuliao:''})
      this.setData({
          viewList: currentList,
          entry:currentList
      })
    },
    onSubmitSuccess(e){
      var that = this
      let tmp = that.data.viewList
      tmp[e.tmp2].sel_wuliao = e.tmp1[0].material_name
      tmp[e.tmp2].FUnitID = e.tmp1[0].FUnitID
      tmp[e.tmp2].FBatchNo = e.tmp1[0].material_name
      tmp[e.tmp2].FItemID = e.tmp1[0].FItemID
      that.setData({
        viewList:tmp,
        entry:tmp
      })
    }
    
})